﻿using ShareFrame.Business.Base_Manage;
using ShareFrame.Entity;
using ShareFrame.Entity.Base_Manage;
using ShareFrame.Util;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_Manage;

namespace ShareFrame.Api.Controllers.Base_Manage
{
    /// <summary>
    /// 系统权限管理控制器
    /// </summary>
    /// <seealso cref="ShareFrame.Api.BaseApiController" />
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("系统权限管理接口", Description = "用于系统权限表的相关增删改查操作")]
    [ApiExplorerSettings(GroupName = "系统管理")]
    public class Base_ActionController : BaseApiController
    {
        #region DI

        public Base_ActionController(IBase_ActionBusiness actionBus)
        {
            _actionBus = actionBus;
        }

        IBase_ActionBusiness _actionBus { get; }

        #endregion

        #region 获取
        /// <summary>
        /// 获取指定实体信息
        /// </summary>
        /// <param name="input">传入的指定参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Base_Action> GetTheData(IdInputDTO input)
        {
            return (await _actionBus.GetTheDataAsync(input.id)) ?? new Base_Action();
        }

        /// <summary>
        /// 获取页面权限
        /// </summary>
        /// <param name="input">传入权限信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Action>> GetPermissionList(Base_ActionsInputDTO input)
        {
            input.types = new ActionType[] { Entity.ActionType.权限 };

            return await _actionBus.GetDataListAsync(input);
        }

        /// <summary>
        /// 获取全部权限信息列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Action>> GetAllActionList()
        {
            return await _actionBus.GetDataListAsync(new Base_ActionsInputDTO
            {
                types = new ActionType[] { ActionType.菜单, ActionType.页面, ActionType.权限 }
            });
        }

        /// <summary>
        /// 获取权限菜单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_ActionDTO>> GetMenuTreeList(Base_ActionsInputDTO input)
        {
            input.selectable = true;
            input.types = new ActionType[] { ActionType.菜单, ActionType.页面 };

            return await _actionBus.GetTreeDataListAsync(input);
        }

        /// <summary>
        /// 获取权限树形信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_ActionDTO>> GetActionTreeList(Base_ActionsInputDTO input)
        {
            input.selectable = false;

            return await _actionBus.GetTreeDataListAsync(input);
        }

        #endregion

        #region 提交

        /// <summary>
        /// 保存提交非空验证调用对应方法
        /// </summary>
        /// <param name="data">传入待操作的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveData(ActionEditInputDTO data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await AddData(data);
            }
            else
            {
                await UpdateData(data);
            }
        }

        /// <summary>
        /// 基础添加方法
        /// </summary>
        /// <param name="data">待添加的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_Brand.Add")]
        public async Task AddData(ActionEditInputDTO data)
        {
            await _actionBus.AddDataAsync(data);
        }

        /// <summary>
        /// 基础更新方法
        /// </summary>
        /// <param name="data">待更新的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_Action.Edit")]
        public async Task UpdateData(ActionEditInputDTO data)
        {
            await _actionBus.UpdateDataAsync(data);
        }

        /// <summary>
        /// 基础删除方法
        /// </summary>
        /// <param name="ids">传入指定删除条件进行单项或多项删除</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_Action.Delete")]
        public async Task DeleteData(List<string> ids)
        {
            await _actionBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}